package com.yuntu.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.yuntu.pojo.Dev_User;
import com.yuntu.service.Dev_UserService;
import com.yuntu.utils.PageUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class Dev_UserController {
    private Logger logger = Logger.getLogger(Dev_UserController.class);
    @Autowired
    private Dev_UserService service;

    /**
     * 登录
     * @param session
     * @param devName
     * @param devPassword
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpSession session, @RequestParam("devName") String devName, @RequestParam("devPassword") String devPassword){
        Dev_User user = service.login(devName,devPassword);
        if (user!=null){
            session.setAttribute("login",user);
            return "redirect:/user/getPage";
        }else{
            session.setAttribute("error","账号或者密码不正确");
            return "login";
        }


    }

    /**
     * 分页
     * @param pageIndex
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPage",method = RequestMethod.GET)
    public String getPage(Integer pageIndex, HttpServletRequest request){
        System.out.println("-------------getPage");
        if (pageIndex==null){
            pageIndex=1;
        }
        PageUtil<Dev_User> pageUtil=new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(3);
        service.getPageAll(pageUtil);
        request.setAttribute("pageUtil",pageUtil);
        return "index";
    }

    /**
     * 添加信息
     * @return
     */
    @RequestMapping("/adddo")
    public String adddo(){
        return "add";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Dev_User dev_user){
        dev_user.setModifyDate(new Date());
        if (service.add(dev_user)) {
            return "redirect:/user/getPage";
        }
        return "add";
    }

    /**
     * 修改和查询单个
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/selone/{id}",method = RequestMethod.GET)
    public String selone(@PathVariable String id, Model model){
        Dev_User user = service.selOne(id);
        if (user!=null){
            model.addAttribute(user);
            return "update";
        }
        return "redirect:/user/getPage";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Dev_User dev_user){
        dev_user.setModifyDate(new Date());
        if (service.update(dev_user)){
            return "redirect:/user/getPage";
        }
        return "update";
    }
    /**
     * 删除
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public String delete(@PathVariable String id){
        if (service.delete(id)){
            return "redirect:/user/getPage";
        }
        return "redirect:/user/getPage";
    }
    /**
     * 判断用户名重复
     */
    @ResponseBody
    @RequestMapping("/ucexist.do")
    public Object userCodeIsExit(@RequestParam String devName){
        Map<String,String> map = new HashMap<>();
        if (StringUtils.isNullOrEmpty(devName)){
            map.put("devName","exist");
        }else{
            Dev_User user = service.selectName(devName);
            if (user!=null){
                map.put("devName","exist");
            }else{
                map.put("devName","noexist");

            }
        }
        return JSONArray.toJSONString(map);
    }
}
